home *** CD-ROM | disk | FTP | other *** search
/ BMUG PD-ROM B4 / PD-ROM B4.iso / Entertainment / Fun Stuff / Imo INIT / Imo.source / Imo.c next >
Encoding:
C/C++ Source or Header  |  1991-04-14  |  13.7 KB  |  640 lines  |  [TEXT/KAHL]

  1. /*--------------------------------------------------------------------------
  2.     filename    Imo.c
  3.     function    Imo    Joke INIT code resource
  4.     author        1991.4.6.-4.14. K.Nakagawa
  5.     change
  6.     note        Developed on Think C ver.4.0.2
  7.                 This version is valid only for International Use
  8.                 No guarantees or waranties of any kind To use this software
  9.                 Copyleft or Copyright is reserved by K.Nakagawa
  10.                 (NIFTY serve PAG02107,IKU-NET(KOBE Japan) 287)
  11. --------------------------------------------------------------------------*/
  12.  
  13. /*--------------------------------------------------------------------------
  14.     include file
  15. --------------------------------------------------------------------------*/
  16.  
  17. static __GetA4(void);
  18. #include <SetUpA4.h>
  19. #include <oops.h>
  20.  
  21. /*--------------------------------------------------------------------------
  22.     definition of constant
  23. --------------------------------------------------------------------------*/
  24.  
  25. #define    NULL ((void    *)0)
  26.  
  27. /* ID number of STR resource    */
  28. #define    START_POINT_ID        1001    /* start position of strings */
  29. #define    VERTICAL_PITCH_ID    1002    /* CR pitch of strings */
  30. #define    DELAY_TIME_ID        1003    /* wait time after 1 line displayed    */
  31. #define    BLINK_TIME_ID        1004    /* blink time of cursor */
  32. #define    CURSOR_SIZE_ID        1005    /* size of cursor */
  33. #define    MESSAGE_START_ID    1100    /* start message ID number */
  34.  
  35. /*--------------------------------------------------------------------------
  36.     definition of types
  37. --------------------------------------------------------------------------*/
  38.  
  39. typedef    struct
  40. {
  41.     BitMap    *sBitMap;    /* bitmap definition of source */
  42.     int        left;        /* left position of image to escape */
  43.     int        top;        /* top position of image to escape */
  44.     int        right;        /* right position of image to escape */
  45.     int        bottom;        /* bottom position of image to escape */
  46.     BitMap    dBitMap;    /* bitmap definition of destination */
  47. } SaveBitMapType;
  48.  
  49. typedef    struct QuickDraw
  50. {
  51.     char    private[76];
  52.     long    randSeed;
  53.     BitMap    screenBits;
  54.     Cursor    arrow;
  55.     Pattern    dkGray;
  56.     Pattern    ltGray;
  57.     Pattern    gray;
  58.     Pattern    black;
  59.     Pattern    white;
  60.     GrafPtr    thePort;
  61. } QuickDraw;
  62.  
  63. /*
  64.     private member of TextDrawClass
  65.     aXpos = horizontal position of character,
  66.     aYpos = vertical position of character,
  67.     aVpitch = linefeed pitch
  68.  
  69.     public member of TextDrawClass
  70.     Init(position of character,linefeed pitch) = setup the object
  71.     Perform(ID number of STR resource) = perform joke
  72.                                 0=OK,-1=error occurred
  73. */
  74. struct TextDrawClass : indirect    {
  75. /* private:    */
  76.     int        aXpos,aYpos,aVpitch;
  77.     void    Scanf(void);
  78. /* public: */
  79.     void    Init(Point pStartPos,int pVpitch);
  80.     int        Perform(int pID);
  81. };
  82.  
  83. /*
  84.     private member of BlinkCursorClass
  85.     aInvert = if true,object is inverted
  86.     aNextTick = time limit of current state
  87.     aBlinkTime = blink period
  88.     aXsize,aYsize = size of cursor
  89.     public member
  90.     Init(blink period,size of cursor) = setup the object
  91.     Idle() = show blink of cursor
  92.     Hide() = hide cursor
  93. */
  94. struct BlinkCursorClass    : indirect {
  95. /* private:    */
  96.     Boolean    aInvert;
  97.     long    aNextTick;
  98.     int        aBlinkTime;
  99.     int        aXsize,aYsize;
  100.     void    Invert(void);
  101. /* public: */
  102.     void    Init(int pBlinkTime,Point pSize);
  103.     void    Idle(void);
  104.     void    Hide(void);
  105. };
  106.  
  107. /*--------------------------------------------------------------------------
  108.     global variables
  109. --------------------------------------------------------------------------*/
  110.  
  111. static QuickDraw      *gQD;        /* pointer to own QuickDraw enviroment */
  112.  
  113. /*--------------------------------------------------------------------------
  114.     prototype definition
  115. --------------------------------------------------------------------------*/
  116.  
  117. void main(void);
  118. void RealMain(void);
  119. static Boolean IsShiftPress(void);
  120. int    SaveBitMap(SaveBitMapType *pSaveBitMap);
  121. void RestoreBitMap(SaveBitMapType *pSaveBitMap);
  122. static int SetupOffBitMap(BitMap *pBitMap,int ph,int pv);
  123. static void    DisposOffBitMap(BitMap *pBitMap);
  124. int    GetPreset1(int pID);
  125. Point GetPreset2(int pID);
  126. static int GetPresetSub(Handle pHandle,int pSkip);
  127. static void    Wait(void);
  128.  
  129. /*--------------------------------------------------------------------------
  130.     program definition which uses "define" keyword
  131. --------------------------------------------------------------------------*/
  132.  
  133. /*--------------------------------------------------------------------------
  134.     BlinkCursorClass
  135. --------------------------------------------------------------------------*/
  136.  
  137. void BlinkCursorClass::Init(int    pBlinkTime,Point pSize)
  138. {
  139.     /* local variables */
  140.     
  141.     /* procedures */
  142.     aBlinkTime = pBlinkTime;
  143.     aXsize = pSize.h;
  144.     aYsize = pSize.v;
  145.     aInvert    = false;
  146.     Invert();
  147. }
  148.  
  149. void BlinkCursorClass::Idle(void)
  150. {
  151.     /* local variables */
  152.     
  153.     /* procedures */
  154.     if(aNextTick < TickCount())
  155.         Invert();
  156. }
  157.  
  158. void BlinkCursorClass::Hide(void)
  159. {
  160.     /* local variables */
  161.     
  162.     /* procedures */
  163.     if(aInvert)
  164.         Invert();
  165. }
  166.  
  167. void BlinkCursorClass::Invert(void)
  168. {
  169.     /* local variables */
  170.     Point    aPenCursor;
  171.     Rect    aRect;
  172.  
  173.     /* procedures */
  174.     GetPen(&aPenCursor);
  175.     SetRect(&aRect,
  176.             aPenCursor.h,
  177.             aPenCursor.v - aYsize,
  178.             aPenCursor.h + aXsize,
  179.             aPenCursor.v);
  180.     InvertRect(&aRect);
  181.     aInvert    = !aInvert;
  182.     aNextTick =    TickCount()    + (long)aBlinkTime;
  183. }
  184.  
  185. /*--------------------------------------------------------------------------
  186.     TextDrawClass
  187. --------------------------------------------------------------------------*/
  188.  
  189. void TextDrawClass::Init(Point pStartPos,int pVpitch)
  190. {
  191.     aXpos =    pStartPos.h;
  192.     aYpos =    pStartPos.v;
  193.     aVpitch    = pVpitch;
  194. }
  195.  
  196. int    TextDrawClass::Perform(int pID)
  197. {
  198.     /* local variables */
  199.     StringHandle    aStringHandle;
  200.     StringPtr        aStringPtr;
  201.     int                aStringLength;
  202.     char            aChar;
  203.     Boolean            aCRflag = false,aYenFlag = false;
  204.  
  205.     /* procedures */
  206.     if(NULL    == (aStringHandle =    GetString(pID)))
  207.         return -1;
  208.  
  209.     HLock(aStringHandle);
  210.     aStringPtr = *aStringHandle;
  211.     
  212.     for(aStringLength = *aStringPtr++; aStringLength > 0; aStringLength--)
  213.     {
  214.         aChar = *aStringPtr++;
  215.  
  216.         /* "\"mark procedure */
  217.         if(aYenFlag)
  218.         {
  219.             aYenFlag = false;
  220.             switch(aChar)
  221.             {
  222.             case 'Q':
  223.             case 'q':
  224.                 Scanf();
  225.                 aCRflag = true;
  226.                 break;
  227.             case 'N':
  228.             case 'n':
  229.                 aCRflag = true;
  230.                 break;
  231.             }
  232.         }else{
  233.             if('\\' == aChar)
  234.             {
  235.                 aYenFlag = true;
  236.             }else{
  237.                 DrawChar(aChar);
  238.             }
  239.         }
  240.  
  241.         /* linefeed performance */
  242.         if(aCRflag)
  243.         {
  244.             aCRflag = false;
  245.             MoveTo(aXpos,aYpos += aVpitch);
  246.         }
  247.     }
  248.     HUnlock(aStringHandle);
  249.  
  250.     return 0;
  251. }
  252.  
  253. void TextDrawClass::Scanf(void)
  254. {
  255.     /* local variables */
  256.     EventRecord            aEvent;
  257.     Boolean                aLoop;
  258.     BlinkCursorClass    *aCursor;
  259.     int                    aKeyCode;
  260.  
  261.     /* procedures */
  262.  
  263.     /* flush the events    */
  264.     FlushEvents(everyEvent,0);
  265.  
  266.     /* make BlinkCursor object */
  267.     if(aCursor = new(BlinkCursorClass))
  268.         aCursor->Init(GetPreset1(BLINK_TIME_ID),GetPreset2(CURSOR_SIZE_ID));
  269.  
  270.     for(aLoop =    true; aLoop; )
  271.     {
  272.         /* display cursor blink */
  273.         if(aCursor)
  274.             aCursor->Idle();
  275.  
  276.         /* get key event */
  277.         if(GetOSEvent(keyDownMask,&aEvent))
  278.         {
  279.             /* hide cursor */
  280.             if(aCursor)
  281.                 aCursor->Hide();
  282.  
  283.             /* linefeed performance or key_punch performance */
  284.             switch(aKeyCode    = (int)(charCodeMask & aEvent.message))
  285.             {
  286.             case 0x0d: /* Return key */
  287.             case 0x03: /* Enter key */
  288.                 aLoop =    false;
  289.                 break;
  290.  
  291.             default: /*    display ASCII code */
  292.                 if(0x20    <= aKeyCode    && aKeyCode    < 0x7f)
  293.                 {
  294.                     DrawChar(aKeyCode);
  295.                 }
  296.                 break;
  297.             }
  298.         }
  299.     }
  300.  
  301.     /* destroy BlinkCursor object */
  302.     if(aCursor)
  303.         delete(aCursor);
  304. }
  305.  
  306. /*--------------------------------------------------------------------------
  307.     public program
  308. --------------------------------------------------------------------------*/
  309.  
  310. void main(void)
  311. {
  312.     /* local variables */
  313.     Handle        aThisHandle;    /* Handle to This INIT resource */
  314.     Handle        aProcHandle;    /* Handle to PROC resource */
  315.     GrafPort    myPort;
  316.     QuickDraw    qdGlobals;
  317.     Ptr            localA5;
  318.     Ptr            savedA5;
  319.  
  320.     /* procedures */
  321.  
  322.     /* prepare to use code resource    */
  323.     RememberA0();    /* to access resource globals */
  324.     SetUpA4();
  325.     asm    {
  326.         _RecoverHandle;
  327.         move.l    a0,aThisHandle
  328.     }
  329.     HLock(aThisHandle);
  330.  
  331.     /* perform ,if No shift key pressed */
  332.     if(!IsShiftPress())
  333.     {
  334.         /* local variables */
  335.         Rect            aRect;
  336.         SaveBitMapType    aSaveScreen;    /* buffer to escape images */
  337.         int                aSaveFlag;        /* result of SaveBitMap */
  338.  
  339.         /* keep the own graph port */
  340.         asm
  341.         {
  342.             move.l    A5,savedA5
  343.             lea        localA5,A5
  344.             move.l    A5,CurrentA5
  345.         }
  346.         InitGraf(&qdGlobals.thePort);
  347.         OpenPort(&myPort);
  348.  
  349.         /* keep the own QuickDraw global variable */
  350.         gQD    = &qdGlobals;
  351.  
  352.         /* save the images of CRT */
  353.         aSaveScreen.sBitMap    = &(gQD->screenBits);
  354.         aRect =    aSaveScreen.sBitMap->bounds;
  355.         aSaveScreen.left = aRect.left;
  356.         aSaveScreen.right =    aRect.right;
  357.         aSaveScreen.top    = aRect.top;
  358.         aSaveScreen.bottom = aRect.bottom;
  359.         aSaveFlag =    SaveBitMap(&aSaveScreen);
  360.  
  361.         /* black out the CRT */
  362.         FillRect(&aRect,gQD->black);
  363.  
  364.         /* change TEXT mode and Font mode */
  365.         InitFonts();
  366.         TextFont(1); /* set font mode as application font */
  367.         TextMode(srcXor);
  368.  
  369.         /* This is the main procedure!! */
  370.         RealMain();
  371.  
  372.         /* recover the images of CRT */
  373.         if(0 ==    aSaveFlag)
  374.             RestoreBitMap(&aSaveScreen);
  375.  
  376.         /* close the graph port */
  377.         ClosePort(&myPort);
  378.         asm
  379.         {
  380.             move.l    savedA5,A5
  381.             move.l    A5,CurrentA5
  382.         }
  383.     }
  384.  
  385.     /* exit    from this code resource    */
  386.     HUnlock(aThisHandle);
  387.     RestoreA4();
  388. }
  389.  
  390. void RealMain(void)
  391. {
  392.     /* local variables */
  393.     TextDrawClass    *aTextDraw;    /* text draw and response object */
  394.     int                aID;        /* ID number of STR resource */
  395.  
  396.     /* procedures */
  397.  
  398.     /* make a text draw object */
  399.     if(NULL    == (aTextDraw =    new(TextDrawClass)))
  400.         return;
  401.     aTextDraw->Init(GetPreset2(START_POINT_ID),GetPreset1(VERTICAL_PITCH_ID));
  402.  
  403.     /* perform and response */
  404.     aID    = MESSAGE_START_ID;
  405.     while(0    == aTextDraw->Perform(aID++))
  406.         Wait();
  407.  
  408.     /* destroy a text draw object */
  409.     delete(aTextDraw);
  410. }
  411.  
  412. /*--------------------------------------------------------------------------
  413.     private program
  414. --------------------------------------------------------------------------*/
  415.  
  416. /*
  417.     get a value of STR resource
  418.     return 0 if any error occurred
  419. */
  420. int    GetPreset1(int pID)
  421. {
  422.     /* local variable    */
  423.     Handle    aHandle;
  424.  
  425.     /* procedure */
  426.     if(NULL    == (aHandle    = GetResource('STR ',pID)))
  427.         return 0;
  428.     return GetPresetSub(aHandle,0);
  429. }
  430.  
  431. /*
  432.     get a coordinate of STR resource
  433.     return 0,0 if any error occurred
  434. */
  435. Point GetPreset2(int pID)
  436. {
  437.     /* local variable    */
  438.     Handle    aHandle;
  439.     Point    aPoint;
  440.  
  441.     /* procedure */
  442.     if(NULL    == (aHandle    = GetResource('STR ',pID)))
  443.     {
  444.         SetPt(&aPoint,0,0);
  445.     }else{
  446.         SetPt(&aPoint,GetPresetSub(aHandle,0),GetPresetSub(aHandle,1));
  447.     }
  448.     return aPoint;
  449. }
  450.  
  451. /*
  452.     convert STR resource which specified by pHandle To number
  453.     pSkip means skip counts of ','mark
  454.     return 0 if any error occurred
  455. */
  456. static int GetPresetSub(Handle pHandle,int pSkip)
  457. {
  458.     /* local variable    */
  459.     int        aResult    = 0;
  460.     int        aCount;
  461.     char    *aPtr,aChar;
  462.     int        aPlusMinus = 1;
  463.  
  464.     /* procedure */
  465.     aPtr = *((char **)pHandle);
  466.     for(aCount = *aPtr++; aCount > 0; aCount--,aPtr++)
  467.     {
  468.         if(pSkip > 0)
  469.         {
  470.             if(*aPtr ==    ',')
  471.                 --pSkip;
  472.         }else{
  473.             switch(aChar = *aPtr)
  474.             {
  475.             case '-':
  476.                 aPlusMinus = -1;
  477.                 break;
  478.             default:
  479.                 if('0' <= aChar    && aChar <=    '9')
  480.                     aResult    = aChar    - '0' +    aResult    * 10;
  481.                 else
  482.                     aCount = 0;
  483.                 break;
  484.             }
  485.         }
  486.     }
  487.     return aPlusMinus *    aResult;
  488. }
  489.  
  490. /*
  491.     save images of CRT
  492.     return 0 if succeeded,-1 if failed
  493.     parameters which you must setup is
  494.     BitMap    *sBitMap;    bitmap definition of source images
  495.     int        left;        left posision of source images
  496.     int        top;        top posision of source images
  497.     int        right;        right posision of source images
  498.     int        bottom;        bottom posision of source images
  499. */
  500. int    SaveBitMap(SaveBitMapType *pSaveBitMap)
  501. {
  502.     /* local variable    */
  503.     /* int        aRowBytes; */
  504.     Rect    aSourceRect,aDestRect;
  505.     /* Ptr        aPtr; */
  506.     BitMap    *aBitMap;
  507.  
  508.     /* procedure */
  509.     aBitMap    = &pSaveBitMap->dBitMap;
  510.  
  511.     SetRect(&aSourceRect,
  512.             pSaveBitMap->left,pSaveBitMap->top,
  513.             pSaveBitMap->right,pSaveBitMap->bottom);
  514.  
  515.     if(0 !=    SetupOffBitMap(aBitMap,
  516.             aSourceRect.right -    aSourceRect.left,
  517.             aSourceRect.bottom - aSourceRect.top))
  518.         return -1;
  519.  
  520.     aDestRect =    aBitMap->bounds;
  521.  
  522.     CopyBits(pSaveBitMap->sBitMap,
  523.              aBitMap,
  524.              &aSourceRect,
  525.              &aDestRect,
  526.              srcCopy,
  527.              NULL
  528.              );
  529.  
  530.     return 0;
  531. }
  532.  
  533. /*
  534.     recover images which saved by SaveBitMap
  535.     pSaveBitMap parameter is same parameter which used by SaveBitMap
  536. */
  537. void RestoreBitMap(SaveBitMapType *pSaveBitMap)
  538. {
  539.     /* local variable    */
  540.     Rect    aSourceRect,aDestRect;
  541.  
  542.     /* procedure */
  543.     SetRect(&aDestRect,
  544.             pSaveBitMap->left,pSaveBitMap->top,
  545.             pSaveBitMap->right,pSaveBitMap->bottom);
  546.  
  547.     SetRect(&aSourceRect,
  548.             0,0,
  549.             aDestRect.right    - aDestRect.left,
  550.             aDestRect.bottom - aDestRect.top);
  551.  
  552.     CopyBits(&pSaveBitMap->dBitMap,
  553.              pSaveBitMap->sBitMap,
  554.              &aSourceRect,
  555.              &aDestRect,
  556.              srcCopy,
  557.              NULL
  558.              );
  559.  
  560.     DisposOffBitMap(&pSaveBitMap->dBitMap);
  561. }
  562.  
  563. /*
  564.     make off_screen_bit_map
  565.     pBitMap is a structure to setup information
  566.     ph is dots of horizontal
  567.     pv is dots of vertical
  568.     return 0 if succeeded,-1 if failed
  569. */
  570. static int SetupOffBitMap(BitMap *pBitMap,int ph,int pv)
  571. {
  572.     /* local variable    */
  573.     register int    aSize;
  574.     int                aRowBytes;
  575.     Rect            aRect;
  576.     register Ptr    aPtr;
  577.     int                aResult;
  578.  
  579.     /* procedure */
  580.     SetRect(&aRect,0,0,ph,pv);
  581.     if((aRowBytes =    ph + 1)    % 16)
  582.         aRowBytes =    (aRowBytes / 16    + 1) * 16;
  583.     aRowBytes /= 8;
  584.  
  585.     pBitMap->rowBytes =    aRowBytes;
  586.     pBitMap->bounds    = aRect;
  587.     aSize =    aRowBytes *    (pv    + 1);
  588.     aPtr = NewPtr(aSize);
  589.     pBitMap->baseAddr =    aPtr;
  590.     if(aPtr    != noErr)
  591.     {
  592.         while(aSize--)
  593.             *aPtr++    = 0;
  594.         return 0;
  595.     }else{
  596.         return -1;
  597.     }
  598. }
  599.  
  600. /*
  601.     release off_screen_bit_map
  602.     pBitMap is a structure to setup information
  603. */
  604. static void    DisposOffBitMap(BitMap *pBitMap)
  605. {
  606.     DisposPtr(pBitMap->baseAddr);
  607. }
  608.  
  609. /*
  610.     return true if SHIFT key pressed
  611. */
  612. static Boolean IsShiftPress(void)
  613. {
  614.     /* local variable    */
  615.     KeyMap    aKeyMap;
  616.  
  617.     /* procedure */
  618.     GetKeys(&aKeyMap);
  619.     return 0L != (aKeyMap.Key[1] & 1L);
  620. }
  621.  
  622. /*
  623.     wait a little time
  624. */
  625. static void    Wait(void)
  626. {
  627.     /* local variable    */
  628.     static int    aWaitTick =    -1;
  629.     long        aDummy;
  630.  
  631.     /* procedure */
  632.     if(aWaitTick < 0)
  633.         aWaitTick =    GetPreset1(DELAY_TIME_ID);
  634.     Delay((long)aWaitTick,&aDummy);
  635. }
  636.  
  637. /*--------------------------------------------------------------------------
  638.     end of file
  639. --------------------------------------------------------------------------*/
  640.